﻿using System.Collections.Generic;
using System.Linq;
using LBoL.Base;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.Core.Cards;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;

namespace ProbeMod.Cards
{
    public sealed class ProbeShieldOverchargeDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Uncommon;
            config.Type = CardType.Defense;
            config.TargetType = TargetType.Nobody;

            config.Colors = new List<ManaColor>() { ManaColor.Blue,ManaColor.White };
            config.Cost = new ManaGroup() { Blue = 1, White = 1, Any = 1 };

            config.Shield = 5;
            config.UpgradedShield = 8;

            config.Keywords = Keyword.Exile;
            config.UpgradedKeywords = Keyword.Exile;

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    /// <summary>
    /// 弃一张牌，如果是建筑牌则每点耐久值获得5/8护盾
    /// </summary>
    [EntityLogic(typeof(ProbeShieldOverchargeDef))]
    public sealed class ProbeShieldOvercharge : ProbeCard
    {
        private Card oneTargetHand;

        public override bool DiscardCard => true;

        public override Interaction Precondition()
        {
            List<Card> list = base.Battle.HandZone.Where((Card hand) => hand != this).ToList();
            if (list.Count == 1)
            {
                oneTargetHand = list[0];
            }
            if (list.Count <= 1)
            {
                return null;
            }
            return new SelectHandInteraction(1, 1, list);
        }

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            if (precondition != null)
            {
                Card card = ((SelectHandInteraction)precondition).SelectedCards[0];
                if (card != null)
                {
                    yield return new DiscardAction(card);
                    if (card.Config.Type == CardType.Friend)
                    {
                        for (int i = 0; i < card.Loyalty; i++)
                        {
                            yield return DefenseAction();
                        }
                    }
                }
            }
            else if (oneTargetHand != null)
            {
                yield return new DiscardAction(oneTargetHand);
                if(oneTargetHand.Config.Type == CardType.Friend)
                {
                    for (int i = 0; i < oneTargetHand.Loyalty; i++)
                    {
                        yield return DefenseAction();
                    }
                }
                oneTargetHand = null;
            }
            
        }
    }
}
